home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-08-16 | 4.0 KB | 124 lines | [TEXT/MPS ] |
- #---------------------------------------------------------------------------------------------------------------------------------------------------
- # CapFirst
- # MPW Shell Script
- # Written by Gina Cherry • August 16,1991
- # Copyright: © 1991 by Apple Computer, Inc., all rights reserved.
- #
- # Usage: CapFirst [file]
- #
- # Function:
- # CapFirst capitalizes the first character of each line of a text file. CapFirst skips over blanks
- # and tabs. If the first character is not a letter, no change occurs.
- #
- # Note: CapFirst can be added to the Edit menu with the following command:
- #
- # AddMenu Edit 'CapFirst/6' 'CapFirst "{Active}"'
- #---------------------------------------------------------------------------------------------------------------------------------------------------
-
- # If there is more than 1 parameter, write error message and exit script.
- If {#} > 1
- Echo "###Usage: {0} File"
- Exit 1
- End >> Dev:StdErr
-
- # Do not exit on error.
- Set Exit 0
-
- # Searches should be case sensitive.
- Set CaseSensitive 1
-
- # If a parameter is given, File is set to the parameter. Otherwise, File is the target file.
- If {#} == 1
- Set File "{1}"
- Else
- Set File "{Target}"
- End
-
- # Record whether fName is already open.
- Set fullName "`Files -i -f "{File}" ≥ Dev:Null`"
- Set wasOpen `Evaluate "∂`Windows∂`" =~ /≈{fullName}≈/`
-
- # Open input file. Since diagnostic output from Open command is not needed, redirect it to bit bucket.
- Open "{File}" ≥ Dev:Null
-
- # If input file does not exist, exit script.
- If {Status} != 0
- Echo "###{0}: {File} is not a valid file."
- Exit 1
- End >> Dev:StdErr
-
- # Set nLines to number of lines of text in input file.
- Set nLines `count -l "{File}"`
-
- # Mark currently selected text to restore state of window at end of script.
- Mark -y § {0}.Selection "{File}"
-
- # Position cursor at beginning of input file.
- Find • "{File}"
-
- # Initialize counter variable.
- Set Count 1
-
- # Save value of NewWindowRect to restore later.
- Set OldWindowRect "{NewWindowRect}"
-
- # Want the workspace window to be small.
- Set NewWindowRect 0,0,100,100
-
- # Open a {0}.Temporary file for workspace.
- Open -n -t {0}.Temp
-
- # Restore value of NewWindowRect.
- Set NewWindowRect "{OldWindowRect}"
-
- # Loop through lines in input file.
- Loop
-
- # Break if no more lines.
- Break If {Count} > {nLines}
-
- # Position cursor at beginning of the current line in input file.
- Find Δ{Count} "{File}"
-
- # Skip over taps and spaces, and select first character.
- Find /[¶t ]/ "{File}"
-
- # Increment the counter variable.
- Evaluate Count += 1
-
- # Check whether the selected character is a lowercase letter.
- # Note: Diagnostic output for the entire If statement is discarded because the If statement will
- # produce an error message if the selected text in the input file is a parenthesis. This occurs
- # because the shell interprets the character as an unmatched parenthesis.
- If "`catenate "{File}.§"`" =~ /[a-z]/
- # If so, change it to an uppercase letter and echo the change to the temporary file,
- # overwriting the file's contents.
- Translate a-z A-Z < "{File}.§" > {0}.Temp
-
- # Select the entire temporary file (a single uppercase letter).
- Find •:∞ {0}.Temp
-
- # Copy the contents of the temporary file to the clipboard.
- Copy § {0}.Temp
-
- # Write the uppercase letter from the clipboard to the input file, overwriting the
- # original lowercase letter.
- Paste § "{File}"
-
- End ≥ Dev:Null
-
- End
-
- # Close the temporary file, don't save changes.
- Close -n {0}.Temp
-
- # Select the text that was originally selected in the input file.
- Find {0}.Selection "{File}"
-
- # Delete the marker from the input file.
- Unmark {0}.Selection "{File}"
-
- # If the input file was not open to start with, close the file and save changes.
- If !"{wasOpen}"
- Close -y "{File}"
- End